package com.gxwz.document.business.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.gxwz.document.core.pagination.Result;
import com.gxwz.document.business.entity.User;
import com.gxwz.document.business.service.IuserService;
import com.gxwz.document.core.action.BaseAction;

@SuppressWarnings("serial")
public class UserAction extends BaseAction{
	protected static final String LIST_JSP = "/WEB-INF/page/systemManage/User_list.jsp";
	protected static final String ADD_JSP = "/WEB-INF/page/systemManage/User_add.jsp";
	protected static final String UPDATE_JSP = "/WEB-INF/page/systemManage/User_update.jsp";
	
	private User user;
	private List<User> ListUser;
	HttpSession session = ServletActionContext.getRequest().getSession();
	private int totalRow  = 0;
	
	@Autowired
	private IuserService userService;
	

	/**
	 * 用户管理分页查询
	 */
	public String list()throws Exception{

		//page 下一页
		//row 完全显示的条数 一般等于（page-1）*默认分页条数
		//
		
		row = (page-1)*DEFUALT_PAGE_SIZE; 
		
		totalRow = userService.getRowNum();
		if(page>(userService.getRowNum()/DEFUALT_PAGE_SIZE+1)){
			//如果下一页大于尾页就减一，默认都是+1进来的
			page--;
		}
		if(row > userService.getRowNum() || maxpage == 1){
			//如果row 超过总记录数就查尾页数据
			row = userService.getRowNum()-(userService.getRowNum()%DEFUALT_PAGE_SIZE);
			ListUser = userService.findByPage(row,DEFUALT_PAGE_SIZE);
		}else if(user != null){
			//模糊查询方法
			ListUser = userService.findByOder(user, row, DEFUALT_PAGE_SIZE);
		}else{
			//正常下一页方法，以row为分界查后面五条数据
			ListUser = userService.findByPage(row,DEFUALT_PAGE_SIZE);
		}
		setForwardView(LIST_JSP);
		return SUCCESS;
	}
	
	/**
	 * 用户添加
	 * @return
	 * @throws Exception 
	 */
	public String add() throws Exception{
		user.setUserPassword("123456");
		userService.add(user);
		return list();
		}
	
	/**
	 * 用户修改
	 * @return
	 * @throws Exception 
	 */
	public String update() throws Exception{
		userService.update(user);
		return list();
		}
	
	/**
	 * 用户删除
	 */
	public String delete() throws Exception{
		userService.delete(user.getUserId());
		return list();
	}
	
	/**
	 * 跳转到用户列表页面
	 * @return
	 */
	public String openlist(){
		forwardView = LIST_JSP;
		ListUser = userService.findByPage(0, 5);
		return SUCCESS;
	}
	
	/**
	 * 跳转到用户添加页面
	 * @return
	 */
	public String openAdd(){
		forwardView = ADD_JSP;
		return SUCCESS;
	}
	
	/**
	 * 跳转到用户修改页面
	 * @return
	 */
	public String openUpdate(){
		user=userService.findById(user.getUserId());
		forwardView = UPDATE_JSP;
		return SUCCESS;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getListUser() {
		return ListUser;
	}

	public List<User> setListUser(List<User> listUser) {
		return ListUser = listUser;
	}

	public int getTotalRow() {
		return totalRow;
	}

	public void setTotalRow(int totalRow) {
		this.totalRow = totalRow;
	}

}
